package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入书的名字：");
        String name = sc.nextLine();
        System.out.println("请输入作者：");
        String author = sc.next();
        System.out.println("请输入类型：");
        String type = sc.next();
        System.out.println("请输入价格：");
        double price = sc.nextDouble();
        Book addbook = new Book(name, author, type, price);

        //判断是否需要扩容
        if (bookList.getSize() == bookList.getCapacity()) {
            int newCapacity = bookList.getCapacity() * 2;
            Book[] newBooks = new Book[newCapacity];
            Book[] books = bookList.getBooks();
            for(int i = 0; i < bookList.getSize(); i++){
                newBooks[i] = books[i];
            }
            bookList.setBooks(newBooks);
        }
        bookList.setBook(bookList.getSize(), addbook);
        bookList.setSize(bookList.getSize() + 1);
    }
}
